package com.gitee.qdbp.base.utils;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import javax.mail.internet.MimeUtility;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.multipart.MultipartFile;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.able.model.file.InputData;
import com.gitee.qdbp.able.result.ResultCode;
import com.gitee.qdbp.tools.utils.VerifyTools;
import eu.bitwalker.useragentutils.Browser;

/**
 * 文件上传处理
 *
 * @author zhaohuihua
 * @version 160218
 */
public abstract class FileIoTools {

    public static InputData getInputData(MultipartFile file) throws ServiceException {
        if (file == null || file.getSize() == 0 || VerifyTools.isBlank(file.getName())) {
            return null;
        }
        try {
            return new InputData(file.getOriginalFilename(), file.getInputStream(), file.getSize());
        } catch (IOException e) {
            throw new ServiceException(ResultCode.SERVER_INNER_ERROR, e);
        }
    }

    public static List<InputData> getInputData(MultipartFile[] files) throws ServiceException {
        try {
            List<InputData> list = null;
            if (files != null && files.length > 0) {
                list = new ArrayList<>();
                for (MultipartFile file : files) {
                    if (file == null || file.getSize() == 0 || VerifyTools.isBlank(file.getName())) {
                        continue;
                    }
                    list.add(new InputData(file.getOriginalFilename(), file.getInputStream(), file.getSize()));
                }
            }
            return list;
        } catch (IOException e) {
            throw new ServiceException(ResultCode.SERVER_INNER_ERROR, e);
        }
    }

    /**
     * 转换为附件下载描述, 防止中文文件名乱码<br>
     * response.setHeader("content-disposition", toAttachmentDesc(request, filename));
     *
     * @param request
     * @param filename
     * @return
     * @throws ServiceException
     * @throws IOException
     */
    public static String toAttachmentDesc(HttpServletRequest request, String filename)
            throws ServiceException, IOException {
        String prefix = "attachment;filename";
        String utf8name = URLEncoder.encode(filename, "UTF-8");
        // 如果没有UA, 则默认使用IE的方式进行编码, 因为毕竟IE还是占多数的
        String newname = prefix + "=\"" + utf8name + "\"";
        String userAgent = request.getHeader("User-Agent");
        if (userAgent != null) {
            Browser group = Browser.parseUserAgentString(userAgent);
            if (group == Browser.OPERA || group == Browser.FIREFOX) {
                // Opera浏览器只能采用filename*
                // FireFox浏览器, 可以使用MimeUtility或filename*或ISO编码的中文输出
                newname = prefix + "*=UTF-8''" + utf8name;
            } else if (group == Browser.SAFARI) {
                // Safari浏览器, 只能采用ISO编码的中文输出
                newname = prefix + "=\"" + new String(filename.getBytes("UTF-8"), "ISO8859-1") + "\"";
            } else if (group == Browser.CHROME) {
                // Chrome浏览器, 只能采用MimeUtility编码或ISO编码的中文输出
                String name = MimeUtility.encodeText(filename, "UTF8", "B");
                newname = prefix + "=\"" + name + "\"";
            }
        }
        return newname;
    }

}
